<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">系统名称</label>
        <el-input v-model="query.appname" clearable placeholder="系统名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">所属商业项目</label>
        <el-input v-model="query.projid" clearable placeholder="所属商业项目" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="系统名称" prop="appname">
            <el-input v-model="form.appname" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="系统描述" prop="appdesc">
            <el-input v-model="form.appdesc" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="商业项目" prop="projid">
            <el-select
              v-model="form.projid"
              filterable
              remote
              reserve-keyword
              :remote-method="remoteMethod"
              :loading="loading"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.projname"
                :value="item.projid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="系统代码" prop="syscode">
            <el-input v-model="form.syscode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="系统key" prop="appkey">
            <el-input v-model="form.appkey" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="系统密钥" prop="appscrect">
            <el-input v-model="form.appscrect" style="width: 370px;" />

          </el-form-item>
          <el-form-item label="系统类型" prop="apptype">
            <el-select v-model="form.apptype" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.apptype"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="appname" label="系统名称" />
        <el-table-column prop="appdesc" label="系统描述" />
        <el-table-column prop="projname" label="所属商业项目" />
        <el-table-column prop="syscode" label="系统代码" />
        <el-table-column prop="apptype" label="系统类型">
          <template slot-scope="scope">
            {{ dict.label.apptype[scope.row.apptype] }}
          </template>
        </el-table-column>
        <el-table-column prop="moduser" label="修改人" />
        <el-table-column prop="modtime" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','baseSysAppinfo:edit','baseSysAppinfo:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBaseSysAppinfo from '@/api/appinfo/baseSysAppinfo'
import { getByNames } from '@/api/appinfo/baseSysProjinfo'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { appid: null, appname: null, appdesc: null, projid: null, syscode: null, appkey: null, appscrect: null, apptype: null, flag: null, notes: null, reguser: null, regtime: null, moduser: null, modtime: null }
export default {
  name: 'BaseSysAppinfo',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['apptype'],
  data() {
    return {
      options: [], // 选择projinfo
      loading: false,

      permission: {
        add: ['admin', 'baseSysAppinfo:add'],
        edit: ['admin', 'baseSysAppinfo:edit'],
        del: ['admin', 'baseSysAppinfo:del']
      },
      rules: {
        appname: [
          { required: true, message: '系统名称不能为空', trigger: 'blur' }
        ],
        appdesc: [
          { required: true, message: '系统描述不能为空', trigger: 'blur' }
        ],
        projid: [
          { required: true, message: '所属商业项目不能为空', trigger: 'blur' }
        ],
        syscode: [
          { required: true, message: '系统代码不能为空', trigger: 'blur' }
        ],
        appkey: [
          { required: true, message: '系统key不能为空', trigger: 'blur' }
        ],
        appscrect: [
          { required: true, message: '系统密钥不能为空', trigger: 'blur' }
        ],
        apptype: [
          { required: true, message: '系统类型不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'appname', display_name: '系统名称' },
        { key: 'projid', display_name: '所属商业项目' }
      ]
    }
  },
  created() {
    // 得到完整数据
    console.log(this.dict.apptype)
    // 打印简化后的label数据
    console.log(this.dict.apptype.label)
  },
  cruds() {
    return CRUD({ title: '应用:系统信息', url: 'api/appinfo/baseSysAppinfo', idField: 'appid', sort: 'appid,desc', crudMethod: { ...crudBaseSysAppinfo }})
  },
  methods: {
    [CRUD.HOOK.afterToCU](crud, form) {
      this.options = []
      this.remoteMethod(form.projname)
      this.form.appkey = this.getuuid()
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    remoteMethod(query) {
      console.log('query:' + query)
      if (query !== '') {
        this.loading = true
        getByNames(query).then((res) => {
          this.loading = false
          this.options = res.filter(item => {
            return item.projname.toLowerCase()
              .indexOf(query.toLowerCase()) > -1
          })
          console.log(res)
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.options = []
      }
    },
    S4() {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
    },
    getuuid() {
      return (this.S4() + this.S4() + '-' + this.S4() + '-' + this.S4() + '-' + this.S4() + '-' + this.S4() + this.S4() + this.S4())
    }
  }
}
</script>

<style scoped>

</style>
